#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;
using std::function;

int add(int x, int y)
{
    cout << "int add(int, int)" << endl;
    return x + y;
}

int multiply(int x, int y, int z)
{
    cout << "int multiply(int, int, int)" << endl;
    return x * y * z;
}

class Example
{
public:
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }
};

void test()
{
    //add函数类型(标签、形态):函数的返回类型 + 参数列表(包括：参数的
    //个数、参数的类型、参数的顺序)
    //add的类型：int(int, int)
    //f的类型：int()
    /* auto f = bind(&add, 1, 2); */
    function<int()> f = bind(&add, 1, 2);
    cout << "f() = " << f() << endl;

    cout << endl;
    //multiply:int(int, int, int)
    //f2的类型：int()
    function<int()> f2 = bind(&multiply, 3, 4, 5);
    cout << "f2() = " << f2() << endl;

    cout << endl;
    Example ex;
    //成员函数add:int(Example *, int, int)
    //f3:int()
    function<int()> f3 = bind(&Example::add, &ex, 10, 20);
    cout << "f3() = " << f3() << endl;

    cout << endl;
    //占位符
    //add的类型：int(int, int)
    //f4的类型：int(int)
    function<int(int)> f4 = bind(&add, 10, std::placeholders::_1);
    cout << "f4(40) = " << f4(40) << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

